# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='English'
_gemname_lowercase='english'
pkgname="ruby-${_gemname_lowercase}"
pkgver=0.7.1
pkgrel=5
pkgdesc="Require 'English.rb' to reference global variables with less cryptic names"
arch=('any')
url="https://github.com/ruby/${_gemname}"
license=('BSD' 'RUBY')
depends=('ruby')
checkdepends=('ruby-rake')
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('ef21413f9b53e249e66aeb82f77678824de3f233d93735fad5526ce9754cc7647db256367d45648d7e76a1b727ad91d7cefce94ab183c878e1dc66d881c0e98b')
b2sums=('9e40dd48934ab7eab42b6f152a57beafdb49a25b20afa34d102e48f19137113aac83d72121c0f2eaf51d3c95cac2ca37a0bc4af1afa084e7d509488087136bc7')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"

  # we build based on a tar archive, not a git repo
  sed --in-place --regexp-extended 's|git ls-files -z|find . -type f -not -path "*/\.git/*" -printf "%P\\\\0"|' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"
  local _platform="$(gem env platform | cut -d':' -f2)"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  install --verbose --directory --mode=0755 \
    "tmp_install_default/gemspec/specifications/gems/${_gemname_lowercase}-${pkgver}" \
    "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "tmp_install${_gemdir}/specifications/default"

  gem build "${_gemname}.gemspec"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${_gemname_lowercase}-${pkgver}.gem"

  gem install \
    --default \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install_default/gemspec" \
    --bindir "tmp_install_default/usr/bin" \
    "${_gemname_lowercase}-${pkgver}.gem"

  mv --verbose "tmp_install_default/gemspec/specifications/default/${_gemname_lowercase}-${pkgver}.gemspec" "tmp_install${_gemdir}/specifications/default/${_gemname_lowercase}-${pkgver}.gemspec"
  mv --verbose "tmp_install${_gemdir}/gems/${_gemname_lowercase}-${pkgver}/lib/${_gemname}.rb" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_gemname}.rb"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install${_gemdir}/cache/" \
    "tmp_install${_gemdir}/build_info/" \
    "tmp_install${_gemdir}/extensions/" \
    "tmp_install${_gemdir}/gems/" \
    "tmp_install${_gemdir}/plugins/" \
    "tmp_install${_gemdir}/specifications/${_gemname_lowercase}-${pkgver}.gemspec" \
    "tmp_install${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"
}

check() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake test
}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
